/*
 * Copyright (c) 2002-2006 by OpenSymphony
 * All rights reserved.
 */
package com.bluestone;

import com.bluestone.Script;
import com.bluestone.config.ActionConfig;
import com.bluestone.scripts.ActionScript;


import java.io.Serializable;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;


public class DefaultActionProxy implements ActionProxy, Serializable {
	
	private static final long serialVersionUID = 3293074152487468527L;


    protected ActionConfig actionConfig;
    protected ActionScript actionScript;
    protected ActionInvocation invocation;
    protected String actionName;
    protected String namespace;
    protected String method;
    protected boolean executeResult;
    protected boolean cleanupContext;

    protected ObjectFactory objectFactory;

    protected ActionEventListener actionEventListener;

    /**
     * This constructor is private so the builder methods (create*) should be used to create an DefaultActionProxy.
     * <p/>
     * The reason for the builder methods is so that you can use a subclass to create your own DefaultActionProxy instance
     * (like a RMIActionProxy).
     */
    protected DefaultActionProxy(ActionInvocation inv, String namespace, String actionName, String methodName, boolean executeResult, boolean cleanupContext) {
        
        this.invocation = inv;
		this.cleanupContext = cleanupContext;
		this.actionName = actionName;
		this.namespace = namespace;
		this.executeResult = executeResult;
        this.method = methodName;
    }
    

    public void setObjectFactory(ObjectFactory factory) {
        this.objectFactory = factory;
    }
    

    public void setActionScript(ActionScript actionScript) {
        this.actionScript = actionScript;
    }
    

    public void setActionEventListener(ActionEventListener listener) {
        this.actionEventListener = listener;
    }

    public Object getAction() {
        return invocation.getAction();
    }

    public String getActionName() {
        return actionName;
    }

    public ActionScript getActionScript() {
        return actionScript;
    }
    
    public void setExecuteResult(boolean executeResult) {
        this.executeResult = executeResult;
    }

    public boolean getExecuteResult() {
        return executeResult;
    }

    public ActionInvocation getInvocation() {
        return invocation;
    }

    public String getNamespace() {
        return namespace;
    }

    public String execute() throws Exception {
        ActionContext nestedContext = ActionContext.getContext();
        ActionContext.setContext(invocation.getInvocationContext());

        String retCode = null;

        String profileKey = "execute: ";
        try {       	
            retCode = invocation.invoke();
        } finally {
            if (cleanupContext) {
                ActionContext.setContext(nestedContext);
            }
        }

        return retCode;
    }


    public String getMethod() {
        return method;
    }



    protected void prepare()  {
        String profileKey = "create DefaultActionProxy: ";
        try {
            invocation.init(this);

        } finally {

        }
    }


	public ActionConfig getConfig() {
		return this.actionConfig;
	}
}
